package com.zjj.distributed.zk.curator.caches;

import com.zjj.distributed.zk.curator.CuratorBaseOperations;
import lombok.extern.slf4j.Slf4j;
import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.recipes.cache.PathChildrenCache;
import org.apache.curator.framework.recipes.cache.PathChildrenCacheEvent;
import org.apache.curator.framework.recipes.cache.PathChildrenCacheListener;
import org.junit.Test;

/**
 * @author zhanglei.zjj
 * @description PathChildrenCache 会对子节点进行监听，但是不会对二级子节点进行监听
 * @date 2023/6/20 21:23
 */
@Slf4j
public class PathCacheTest extends CuratorBaseOperations {
    public static final String PATH = "/path-cache";

    @Test
    public void testPathCache() throws Exception {

        createIfNeed(PATH, "pathCache");
        PathChildrenCache pathChildrenCache = new PathChildrenCache(curatorFramework, PATH, true);
        pathChildrenCache.getListenable().addListener(new PathChildrenCacheListener() {
            @Override
            public void childEvent(CuratorFramework client, PathChildrenCacheEvent event) throws Exception {
                log.info("event:  {}", event);
            }
        });

        // 如果设置为true则在首次启动时就会缓存节点内容到Cache中
        pathChildrenCache.start(true);
    }
}
